package com.etc.cafka;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;
import java.util.UUID;


public class ProducerDemo {
    public static void main(String[] args)  {
        Properties props = new Properties();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.88.5:9092");
       //        props.put(ProducerConfig.CLIENT_ID_CONFIG, "DemoProducer");
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
        String dataKey = UUID.randomUUID().toString();
        String dataValue = UUID.randomUUID().toString();

        ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>("test", "sd", "我d是光村滴");

        //第一种发送方式 同步
        //        for (int i = 0; i < 10; i++) {
        //            producer.send(producerRecord).get();
        //            System.out.println("发送"+i+"条");
        //        }
        //第二种发送方式 异步
//        producer.send(producerRecord);
        //第三种  Callback
        producer.send(producerRecord, new Callback() {
            public void onCompletion(RecordMetadata metadata, Exception exception) {
                System.out.println(exception + metadata.topic() + metadata.offset());
            }
        });
    }
}
